package com.miyake.loveviewweb.shared;

public class ArrayThinner {
    public String thin(String csv, int width) {
        String[] arr = csv.split(",");
        if (arr.length < width) {
            return csv;
        }
        else {
            String newcsv = "";  
            double step = (double)arr.length / (double)width;
            for (int i = 0; i < width; i++) {
                int index = (int)(step * (double)i);
                newcsv += arr[index];
                if (i < width - 1) {
                    newcsv += ",";
                }
            }
            return newcsv;
        }
    }
    
    public double[] thin(double[] data, int width) {
        double[] ret = new double[width];
        double step = (double)data.length / (double)width;
        for (int i = 0; i < width; i++) {
            int index = (int)(step * (double)i);
            ret[i] = data[index];
        }
        return ret;    
    }
    
    public String[] thin(String[] data, int width) {
        String[] ret = new String[width];
        double step = (double)data.length / (double)width;
        for (int i = 0; i < width; i++) {
            int index = (int)(step * (double)i);
            ret[i] = data[index];
        }
        return ret;    
    }
}
